﻿using Google.Protobuf.WellKnownTypes;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using PHMEE.ToolKits;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Auditing;

namespace PHMEE.Services.Auth
{
    public class ServerAppService : ApplicationService, IServerAppService
    {
        private readonly HttpClient httpClient;
        private readonly IWebHostEnvironment hostEnvironment;
        private readonly IHttpContextAccessor httpContextAccessor;
        private readonly IHttpClientFactory httpClientFactory;

        public ServerAppService(IWebHostEnvironment hostEnvironment, IHttpContextAccessor httpContextAccessor, IHttpClientFactory httpClientFactory)
        {
            this.hostEnvironment = hostEnvironment;
            this.httpContextAccessor = httpContextAccessor;
            this.httpClientFactory = httpClientFactory;
            httpClient = httpClientFactory.CreateClient();
        }

        /// <summary>
        /// 获取服务器使用信息 🔖
        /// </summary>
        /// <returns></returns>
        public dynamic GetServerBase()
        {
            var data = new
            {
                HostName = Environment.MachineName, // 主机名称
                SystemOs = ComputerUtil.GetOSInfo(),//RuntimeInformation.OSDescription, // 操作系统
                OsArchitecture = Environment.OSVersion.Platform.ToString() + " " + RuntimeInformation.OSArchitecture.ToString(), // 系统架构
                ProcessorCount = Environment.ProcessorCount + " 核", // CPU核心数
                SysRunTime = ComputerUtil.GetRunTime(), // 系统运行时间
                LocalIp = httpContextAccessor.HttpContext?.Connection?.LocalIpAddress?.MapToIPv4()?.ToString() + ":" + httpContextAccessor.HttpContext?.Connection?.LocalPort, // 本地地址
                FrameworkDescription = RuntimeInformation.FrameworkDescription, // NET框架
                Environment = hostEnvironment.IsDevelopment() ? "Development" : "Production",
                Wwwroot = hostEnvironment.WebRootPath, // 网站根目录
            };

            return data;
        }

        /// <summary>
        /// 获取服务器使用信息 🔖
        /// </summary>
        /// <returns></returns>
        public dynamic GetServerUsed()
        {
            var programStartTime = Process.GetCurrentProcess().StartTime;
            var totalMilliseconds = (DateTime.Now - programStartTime).TotalMilliseconds.ToString();
            var ts = totalMilliseconds.Contains('.') ? totalMilliseconds.Split('.')[0] : totalMilliseconds;
            var programRunTime = ComputerUtil.FormatTime(ts.ParseToLong());

            var memoryMetrics = ComputerUtil.GetComputerInfo();
            return new
            {
                memoryMetrics.FreeRam, // 空闲内存
                memoryMetrics.UsedRam, // 已用内存
                memoryMetrics.TotalRam, // 总内存
                memoryMetrics.RamRate, // 内存使用率
                memoryMetrics.CpuRate, // Cpu使用率
                StartTime = programStartTime.PhmeeToFriendlyDateTimeString(), // 服务启动时间
                RunTime = programRunTime, // 服务运行时间
                SysRunTime = ComputerUtil.GetRunTime(), // 系统运行时间
            };
        }

        /// <summary>
        /// 获取服务器磁盘信息 🔖
        /// </summary>
        /// <returns></returns>
        public dynamic GetServerDisk()
        {
            return ComputerUtil.GetDiskInfos();
        }

    }
}
